package com.uniview.dao;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.persistence.Convert;
import java.util.List;
import java.util.Map;

@Repository
public class OrderCountDao {
	
	@Resource
	private  SessionFactory sessionFactory;

	/**
	 * 查询过去days的数据
	 * @param days
	 * @return
	 */
    public List countOrdersFromDay(String empCode,String department,int days) {
    	//普通员工或者工程师
		String sql="select status,count(*) num from SD_ORDERS where status <> -99 and (UPPER(creatorno)='"+empCode+"' or UPPER(handleno)='"+empCode+"') and   createtime between sysdate - "+days+" and sysdate group by status";
		if(isRegionManager(empCode))//服务经理
			sql="select status,count(*) num from SD_ORDERS where status <> -99 and (UPPER(creatorno)='"+empCode+"' or handlerDept like '%"+department+"%') and  createtime between sysdate - "+days+" and sysdate group by status";
		List list = sessionFactory.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		return  list;
    }
    
    /**
	 * 查询过去days的数据
	 * @param days
	 * @return
	 */
    public List countOrdersFromDayByAdmin(int days) {
    	//普通员工或者工程师
		String sql = "select status,count(*) num from SD_ORDERS where status <> -99 and createtime between sysdate - " + days + " and sysdate group by status";
		List list = sessionFactory.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		return  list;
    }

	/**
	 * 判断是否是服务经理
	 * @param empCode
	 * @return
	 */
	private boolean isRegionManager(String empCode){
		String sql="select * from SD_REGIONMANAGER t where UPPER(workno)='"+empCode+"'";
		List list = sessionFactory.getCurrentSession().createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		if(list.size()>0)
			return true;
		return  false;
	}

}

